package com.yc.mybatis.mapper;

import com.yc.mybatis.bean.Hall;
import com.yc.mybatis.bean.Movie;
import com.yc.mybatis.bean.Plan;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface HallMapper {
    void insert(Hall hall);
    Hall selectById(int id);

    @Insert("insert into sp_hall values(#{id},#{name},#{is3d},#{size},#{seats})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insert1(Hall hall);

    @Select(" select * from sp_hall where id=#{id}")
    Hall selectById1(int id);

    // 如果语句中包含 标签, 那么该语句必须用 <script> 包起来
    @Select("<script>        select * from sp_movie\n" +
            "        <where>\n" +
            "            <if test=\"name != null\">\n" +
            "                name like concat('%', #{name}, '%')\n" +
            "            </if>\n" +
            "            <if test=\"type != null\">\n" +
            "                and type = #{type}\n" +
            "            </if>\n" +
            "            <if test=\"intro != null\">\n" +
            "                and intro = concat('%', #{intro}, '%')\n" +
            "            </if>\n" +
            "            <if test=\"is3d != null\">\n" +
            "                and is3d = #{is3d}\n" +
            "            </if>\n" +
            "        </where>" +
            "</script>")
    List<Movie> selectMovieByObject(Movie movie);

    @Select("select * from sp_plan where id=#{id}")
    // resultMap => @Results
    @Results(
            id = "rm",
            value = {
                    // id = true => 主键字段
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "play_time", property = "playTime"),
                    @Result(column = "mid", property = "mid"),
                    @Result(column = "mid", property = "movie",
                        one = @One(select = "com.yc.mybatis.mapper.MovieDao.selectById",
                                   fetchType = FetchType.LAZY)),
            }
    )
    Plan selectPlanById(int id);

    // TODO 请完成 一对多关联查询
    Movie selectMovieById(int id);


}
